const { BaseBehavior, COMPONENT_OPTIONS } = require('../../mixins/component');
const { isValidTime } = require('../../common/utils');
const { getSysInfo, navTo } = require('../../common/platform');
const { BANNER_NAV } = require('../../constants/route');

Component({
    behaviors: [BaseBehavior],
    options: {
        ...COMPONENT_OPTIONS,
    },
    properties: {
        bannerType: {
            type: String,
            value: '16-9',
        },
        bannerList: {
            type: Array,
            value: [],
        },
    },
    data: {
        list: [],
        bannerHeight: 200,
    },
    lifetimes: {
        ready: function () {
            // 根据时间剔除已结束或者未开始的广告
            const { bannerList, bannerType } = this.data;
            console.log(bannerList);
            const { screenWidth } = getSysInfo();
            const ratio = bannerType.split('-');
            const _list = bannerList.filter(item => isValidTime(item.startTime, item.endTime));
            this.setData({
                list: _list,
                bannerHeight: (ratio[1] - 0) * (screenWidth - 32) / (ratio[0] - 0),
            });
        },
    },
    methods: {
        bannerTap(e) {
            const { navtype, ids, name } = e.currentTarget.dataset;
            if (navtype === 'empty') {
                return;
            }
            navTo(BANNER_NAV[navtype], {
                goodsIds: (ids || []).join(','),
                title: name,
            });
        },
    }
})
